package jqgridjavawrapper;

/**
 * @author Isuru Madushanka Weerarathna
 * @contact uisurumadushanka89@gmail.com
 */
public class JQGridColumnFormatter implements JQPropertyMaker {

    private boolean customFormat = false;
    private String customFormatFunctionName = null;

    private boolean integer = false;
    private boolean number = false;
    private boolean currency = false;
    private boolean link = false;
    private boolean email = false;
    private boolean checkBox = false;
    private boolean showlink = false;

    private String decimalSeparator = ".";
    private String thousandSeparator = " ";
    private String defaultValue = "0";
    private int decimalPlaces = 2;
    private String currencyPrefix = "";
    private String currencySuffix = "";

    private String linkTarget = null;

    private String showlinkBaseURL = null;
    private String showlinkShowAction = null;
    private String showlinkAdditionalParam = null;
    private String showlinkIDName = null;

    private boolean checkBoxDisabled = false;

    private int showlinkcount = 0;

    /**
     * @param thousandSeparator the thousandSeparator to set
     */
    public void setThousandSeparator(String thousandSeparator) {
        this.thousandSeparator = thousandSeparator;
    }

    /**
     * @param showlinkShowAction the showlinkShowAction to set
     */
    public void setShowlinkShowAction(String showlinkShowAction) {
        this.showlinkShowAction = showlinkShowAction;
        showlinkcount++;
    }

    /**
     * @param showlinkIDName the showlinkIDName to set
     */
    public void setShowlinkIDName(String showlinkIDName) {
        this.showlinkIDName = showlinkIDName;
        showlinkcount++;
    }

    /**
     * @param showlinkBaseURL the showlinkBaseURL to set
     */
    public void setShowlinkBaseURL(String showlinkBaseURL) {
        this.showlinkBaseURL = showlinkBaseURL;
        showlinkcount++;
    }

    /**
     * @param showlinkAdditionalParam the showlinkAdditionalParam to set
     */
    public void setShowlinkAdditionalParam(String showlinkAdditionalParam) {
        this.showlinkAdditionalParam = showlinkAdditionalParam;
        showlinkcount++;
    }

    /**
     * @param showlink the showlink to set
     */
    public void setShowlink(boolean showlink) {
        this.showlink = showlink;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(boolean number) {
        this.number = number;
    }

    /**
     * @param linkTarget the linkTarget to set
     */
    public void setLinkTarget(String linkTarget) {
        this.linkTarget = linkTarget;
    }

    /**
     * @param link the link to set
     */
    public void setLink(boolean link) {
        this.link = link;
    }

    /**
     * @param integer the integer to set
     */
    public void setInteger(boolean integer) {
        this.integer = integer;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(boolean email) {
        this.email = email;
    }

    /**
     * @param defaultValue the defaultValue to set
     */
    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    /**
     * @param decimalSeparator the decimalSeparator to set
     */
    public void setDecimalSeparator(String decimalSeparator) {
        this.decimalSeparator = decimalSeparator;
    }

    /**
     * @param decimalPlaces the decimalPlaces to set
     */
    public void setDecimalPlaces(int decimalPlaces) {
        this.decimalPlaces = decimalPlaces;
    }

    /**
     * @param currencySuffix the currencySuffix to set
     */
    public void setCurrencySuffix(String currencySuffix) {
        this.currencySuffix = currencySuffix;
    }

    /**
     * @param currencyPrefix the currencyPrefix to set
     */
    public void setCurrencyPrefix(String currencyPrefix) {
        this.currencyPrefix = currencyPrefix;
    }

    /**
     * @param currency the currency to set
     */
    public void setCurrency(boolean currency) {
        this.currency = currency;
    }

    /**
     * @param checkBoxDisabled the checkBoxDisabled to set
     */
    public void setCheckBoxDisabled(boolean checkBoxDisabled) {
        this.checkBoxDisabled = checkBoxDisabled;
    }

    /**
     * @param checkBox the checkBox to set
     */
    public void setCheckBox(boolean checkBox) {
        this.checkBox = checkBox;
    }

    /**
     * @return the showlink
     */
    public boolean isShowlink() {
        return showlink;
    }

    /**
     * @return the number
     */
    public boolean isNumber() {
        return number;
    }

    /**
     * @return the link
     */
    public boolean isLink() {
        return link;
    }

    /**
     * @return the integer
     */
    public boolean isInteger() {
        return integer;
    }

    /**
     * @return the email
     */
    public boolean isEmail() {
        return email;
    }

    /**
     * @return the currency
     */
    public boolean isCurrency() {
        return currency;
    }

    /**
     * @return the checkBoxDisabled
     */
    public boolean isCheckBoxDisabled() {
        return checkBoxDisabled;
    }

    /**
     * @return the checkBox
     */
    public boolean isCheckBox() {
        return checkBox;
    }

    /**
     * @return the thousandSeparator
     */
    public String getThousandSeparator() {
        return thousandSeparator;
    }

    /**
     * @return the showlinkShowAction
     */
    public String getShowlinkShowAction() {
        return showlinkShowAction;
    }

    /**
     * @return the showlinkIDName
     */
    public String getShowlinkIDName() {
        return showlinkIDName;
    }

    /**
     * @return the currencyPrefix
     */
    public String getCurrencyPrefix() {
        return currencyPrefix;
    }

    /**
     * @return the currencySuffix
     */
    public String getCurrencySuffix() {
        return currencySuffix;
    }

    /**
     * @return the decimalPlaces
     */
    public int getDecimalPlaces() {
        return decimalPlaces;
    }

    /**
     * @return the decimalSeparator
     */
    public String getDecimalSeparator() {
        return decimalSeparator;
    }

    /**
     * @return the defaultValue
     */
    public String getDefaultValue() {
        return defaultValue;
    }

    /**
     * @return the linkTarget
     */
    public String getLinkTarget() {
        return linkTarget;
    }

    /**
     * @return the showlinkAdditionalParam
     */
    public String getShowlinkAdditionalParam() {
        return showlinkAdditionalParam;
    }

    /**
     * @return the showlinkBaseURL
     */
    public String getShowlinkBaseURL() {
        return showlinkBaseURL;
    }

    /**
     * @param customFormat the customFormat to set
     */
    public void setCustomFormat(boolean customFormat) {
        this.customFormat = customFormat;
    }

    /**
     * @param customFormatFunctionName the customFormatFunctionName to set
     */
    public void setCustomFormatFunctionName(String customFormatFunctionName) {
        this.customFormatFunctionName = customFormatFunctionName;
    }

    /**
     * @return the customFormatFunctionName
     */
    public String getCustomFormatFunctionName() {
        return customFormatFunctionName;
    }

    /**
     * @return the customFormat
     */
    public boolean isCustomFormat() {
        return customFormat;
    }

    public StringBuilder generateProperties() {
        StringBuilder sb = new StringBuilder();

        if (this.customFormat) {
            if (this.customFormatFunctionName != null) {
                sb.append("formatter:").append(this.customFormatFunctionName);
            }
        }

        if (integer) {
            sb.append("formatter: 'integer'");
            sb.append(", formatoptions: {");
            sb.append("thousandsSeparator:\"").append(this.thousandSeparator).append("\", ");
            sb.append("defaulValue: \"").append(this.defaultValue).append("\"");
            sb.append("}");
        }
        else if (number) {
            sb.append("formatter: 'number'");
            sb.append(", formatoptions: {");
            sb.append("thousandsSeparator:\"").append(this.thousandSeparator).append("\", ");
            sb.append("decimalSeparator:\"").append(this.decimalSeparator).append("\", ");
            sb.append("decimalPlaces:").append(this.decimalPlaces).append(", ");
            sb.append("defaulValue: \"").append(this.defaultValue).append("\"");
            sb.append("}");
        }
        else if (currency) {
            sb.append("formatter: 'currency'");
            sb.append(", formatoptions: {");
            sb.append("thousandsSeparator:\"").append(this.thousandSeparator).append("\", ");
            sb.append("decimalSeparator:\"").append(this.decimalSeparator).append("\", ");
            sb.append("decimalPlaces:").append(this.decimalPlaces).append(", ");
            sb.append("prefix:\"").append(this.currencyPrefix).append("\", ");
            sb.append("suffix:\"").append(this.currencySuffix).append("\", ");
            sb.append("defaulValue: \"").append(this.defaultValue).append("\"");
            sb.append("}");
        }
        else if (link) {
            sb.append("formatter: 'link'");
            if (this.linkTarget != null) {
                sb.append(", formatoptions: {");
                sb.append("target: \"").append(this.linkTarget).append("\"");
                sb.append("}");
            }
        }
        else if (showlink) {
            sb.append("formatter: 'showlink'");
            if (showlinkcount > 0) {
                int tmp = 0;

                sb.append(", formatoptions: {");
                if (this.showlinkBaseURL != null) {
                    sb.append("baseLinkUrl:'").append(this.showlinkBaseURL).append("'");
                    tmp++;
                }
                if (this.showlinkAdditionalParam != null) {
                    if (tmp > 0) {
                        sb.append(",");
                        tmp = 0;
                    }
                    sb.append("addParam:'").append(this.showlinkAdditionalParam).append("'");
                    tmp++;
                }
                if (this.showlinkShowAction != null) {
                    if (tmp > 0) {
                        sb.append(",");
                        tmp = 0;
                    }
                    sb.append("showAction:'").append(this.showlinkShowAction).append("'");
                    tmp++;
                }
                if (this.showlinkIDName != null) {
                    if (tmp > 0) {
                        sb.append(",");
                        tmp = 0;
                    }
                    sb.append("idName: '").append(this.showlinkIDName).append("'");
                    tmp++;
                }
                if (this.linkTarget != null) {
                    if (tmp > 0) {
                        sb.append(",");
                        tmp = 0;
                    }
                    sb.append("target: '").append(this.linkTarget).append("'");
                    tmp++;
                }

                sb.append("}");
            }
        }
        else if (email) sb.append("formatter: 'mail'");
        else if (checkBox) {
            sb.append("formatter: 'checkbox'");
            sb.append(", formatoptions: {");
            sb.append("disabled: ").append(this.checkBoxDisabled);
            sb.append("}");
        }
        else return sb;

        return sb;
    }
}

